package project.pojo;

import java.util.List;

/**
 * @param <T> Java Bean类型
 * @author d11yu
 */
public class Page<T> {

    public static final Integer ENTRY_COUNT_PER_PAGE = 4;

    /**
     * page Number 当前页码
     */
    private Integer pn;

    /**
     * page Total 总页码
     */
    private Integer pt;

    /**
     * entryCountPage 每页条目数量
     */
    private Integer ecp = ENTRY_COUNT_PER_PAGE;

    /**
     * entryCountTotal 总条目数
     */
    private Integer ect;

    /**
     * Page entries 当前页数据
     */
    private List<T> pes;

    /**
     * 分页条的请求地址
     */
    private String url;

    /**
     * action
     */
    private String action;

    /**
     * url and action
     */
    private String urlAction;

    public Page() {
    }

    public Page(Integer pn, Integer pt, Integer ecp, Integer ect, List<T> pes, String url, String action) {
        this.pn = pn;
        this.pt = pt;
        this.ecp = ecp;
        this.ect = ect;
        this.pes = pes;
        this.url = url;
        this.action = action;
        this.urlAction = url + "?" + action;
    }

    public Integer getPn() {
        return pn;
    }

    /**
     * 要先设置pt
     *
     * @param pn
     */
    public void setPn(Integer pn) {
        this.pn = pn;
        // 数据边界的有效检查
        if (this.pn < 1) {
            this.pn = 1;
        }
        if (this.pn > pt) {
            this.pn = pt;
        }
    }

    public Integer getPt() {
        return pt;
    }

    public void setPt(Integer pt) {
        this.pt = pt;
    }

    public Integer getEcp() {
        return ecp;
    }

    public void setEcp(Integer ecp) {
        this.ecp = ecp;
    }

    public Integer getEct() {
        return ect;
    }

    public void setEct(Integer ect) {
        this.ect = ect;
    }

    public List<T> getPes() {
        return pes;
    }

    public void setPes(List<T> pes) {
        this.pes = pes;
    }

    @Override
    public String toString() {
        return "Page{" +
                "pn=" + pn +
                ", pt=" + pt +
                ", ecp=" + ecp +
                ", ect=" + ect +
                ", pes=" + pes +
                ", url='" + url + '\'' +
                ", action='" + action + '\'' +
                ", urlAction='" + urlAction + '\'' +
                '}';
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getUrlAction() {
        return urlAction;
    }

    public void setUrlAction(String urlAction) {
        this.urlAction = urlAction;
        this.url = urlAction.substring(0, urlAction.indexOf("?"));
        this.action = urlAction.substring(urlAction.indexOf("?") + 1);
    }
}
